
/**
 * Dispatches received gesture event to currently focused scene.
 */
var GestureDispatcher = {
    currentScene: null, /**< Current scene object, set it when a scene is focused */

    /**
     * Passes received gesture event to currently focused scene.
     * The scene object needs to have handleGesture() method.
     * @param e Gesture event object
     */
    handleGesture: function(e) {
        log("Dispatching event, type: '" + e.eventtype + "', result: '" + e.result + "'");

        if (GestureDispatcher.currentScene !== null) {
            GestureDispatcher.currentScene.handleGesture(e);
        }
    }
};
var VoiceDispatcher = {
	    currentScene: null, /**< Current scene object, set it when a scene is focused */

	    /**
	     * Passes received voice event to currently focused scene.
	     * The scene object needs to have handleVoice() method.
	     * @param e Gesture event object
	     */
	    handleVoice: function(e) {
	        log("Dispatching event, type: '" + e.eventtype + "', result: '" + e.result + "'");

	        if (VoiceDispatcher.currentScene !== null) {
	            VoiceDispatcher.currentScene.handleVoice(e);
	        }
	    }
	};
/**
 * Setup scene checks for gesture recognition support and subscribes for
 * gesture recognition event. Displays error messages if recognition support is
 * not detected.
 * @constructor
 */
function SceneScene1() {
    this.initialize = function() {
    	$('#PopupMang').sfPopup({
			text:'Mất kết nối internet. Bạn muốn thoát ứng dụng?',
			buttons:['Đồng ý ', 'Không'],
			defaultFocus: 1, 
			callback:function(selectedIndex){
				alert('selected button index: ' + selectedIndex);
				if(selectedIndex==0){
					sf.core.exit();
				}
				else{
					sf.scene.focus('Main');
				}
			}
		});
    };

    this.handleShow = function () {
        log("Scene1.handleShow()");

        // sets key help bar
        

        // checks for WebAPIs

        if (typeof webapis !== "object" || typeof webapis.recognition !== "object") {
            document.getElementById("error").innerHTML = "ERROR: webapis not present";
            return;
        }

        // checks if device supports voice and gesture recognition

        if (webapis.recognition.IsRecognitionSupported() !== true) {
            document.getElementById("error").innerHTML = "ERROR: Recognition not supported";
            return;
        }

        // checks if gesture recognition is enabled

        if (webapis.recognition.IsGestureRecognitionEnabled() !== true) {
            document.getElementById("error").innerHTML = "ERROR: Gesture recognition is not enabled";
            return;
        }

        // subscribes for gesture events

        var subscribeResult = webapis.recognition.SubscribeExEvent(
            webapis.recognition.PL_RECOGNITION_TYPE_GESTURE, "testApp",
            GestureDispatcher.handleGesture);

        if (subscribeResult !== true) {
            document.getElementById("error").innerHTML = "ERROR: Failed subscribing for gesture events";
            return;
        }
        
        if (webapis.recognition.IsVoiceRecognitionEnabled() !== true) {
            document.getElementById("error").innerHTML = "ERROR: Voice recognition is not enabled";
            return;
        }
        
        var subscribeResult1 = webapis.recognition.SubscribeExEvent(
                webapis.recognition.PL_RECOGNITION_TYPE_VOICE, "testApp",
                VoiceDispatcher.handleVoice);

            if (subscribeResult1 !== true) {
                document.getElementById("error").innerHTML = "ERROR: Failed subscribing for voice events";
                return;
            }
        

        // all is fine, show the gallery

        sf.scene.hide("Scene1");
        sf.scene.show("Main");
        sf.scene.focus("Main");
    };

    this.handleHide = function () {
    };

    this.handleFocus = function () {
    };

    this.handleBlur = function () {
    };

    this.handleKeyDown = function (keyCode) {
        log("SceneInit.handleKeyDown(" + keyCode + ")");

        switch (this.state) {
        case sf.key.RETURN:
        case sf.key.EXIT:
            sf.core.exit();
            break;

        default:
            break;
        }
    };
}
